using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;
using umbraco.controls;
using umbraco.cms.businesslogic.language;
using umbraco.BusinessLogic.Actions;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.trees
{
    public class loadMailerTree : BaseTree
    {
        public loadMailerTree(string application) : base(application) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = "init" + TreeAlias;
            rootNode.NodeID = "init";
            rootNode.Menu = new System.Collections.Generic.List<IAction>();
        }

        public override void Render(ref XmlTree tree)
        {
            // get collection values
            Commerce.Common.MessagingMailerCollection mailers = null;

            mailers = new MessagingMailerCollection().Load();

            foreach (MessagingMailer thisMailer in mailers)
            {
                XmlTreeNode xNode = XmlTreeNode.Create(this);
                xNode.NodeID = thisMailer.MailerID.ToString();
                xNode.Text = thisMailer.MailerName;
                xNode.Action = "javascript:openMailer(" + thisMailer.MailerID + ");";
                xNode.Icon = "statistik.gif";
                xNode.OpenIcon = "statistik.gif";

                tree.Add(xNode);            
            }

        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append(
                @"
                    function openMailer(id) {
	                    parent.right.document.location.href = 'commerce/editMailer.aspx?mailer=' + id;
                    }
                ");
        }

        protected override void CreateAllowedActions(ref System.Collections.Generic.List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionRefresh.Instance);
        }

        protected override void CreateRootNodeActions(ref System.Collections.Generic.List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionRefresh.Instance);
        }
    }
}
